//
//  CommentView.m
//  KOFY
//
//  Created by Warren Holybee on 12/4/10.
//  Copyright 2010 Warren Holybee. All rights reserved.
//


#import "DogView.h"
#import "SKPSMTPMessage.h"
#import "NSData+Base64Additions.h"
#import "defines.h"

@implementation DogView
@synthesize emailText;
@synthesize image;
@synthesize button;
@synthesize wheel;

-(IBAction)back: (id) sender {
	
	[[self parentViewController] dismissModalViewControllerAnimated:NO];
}

#pragma mark Email Stuff

-(IBAction)send: (id) sender {

	SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init];
    testMsg.fromEmail = SMTP_FROM;
    testMsg.toEmail = SMTP_TO;
    testMsg.relayHost = SMTP_SERVER;
    testMsg.requiresAuth = NO;
	testMsg.wantsSecure = NO;
	testMsg.subject = @"iPhone dog submission";
	testMsg.delegate = self;
	
	NSLog(@"email text = %@", [emailText text]);
	
	NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain",kSKPSMTPPartContentTypeKey,
                               [emailText text],kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];
 
	NSData *dogData = UIImageJPEGRepresentation(image, 0.4);	
if (dogData != nil) {
	

    NSDictionary *dogPart = [NSDictionary dictionaryWithObjectsAndKeys:@"image/jpg;\r\n\tx-unix-mode=0644;\r\n\tname=\"dog.jpg\"",kSKPSMTPPartContentTypeKey,
                             @"attachment;\r\n\tfilename=\"dog.jpg\"",kSKPSMTPPartContentDispositionKey,[dogData encodeBase64ForData],kSKPSMTPPartMessageKey,@"base64",kSKPSMTPPartContentTransferEncodingKey,nil];
	testMsg.parts = [NSArray arrayWithObjects:plainPart,dogPart,nil];

		} else {  
	
	
	testMsg.parts = [NSArray arrayWithObjects:plainPart,nil];
		}
	[self.wheel startAnimating]; 
	[emailText resignFirstResponder];
	self.view.backgroundColor=[UIColor grayColor];
	
	[testMsg send];
	
}

- (void)messageSent:(SKPSMTPMessage *)message
{
    [message release];
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Thank You" 
													message:@"Your Dog's Photo has been sent." 
												   delegate:self 
										  cancelButtonTitle:@"OK" 
										  otherButtonTitles:nil]; 
	[alert show];
	[alert release];
	[self.wheel stopAnimating];
	self.view.backgroundColor=[UIColor whiteColor];
	[self back:self];
	
    NSLog(@"delegate - message sent");
}

- (void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error
{
    [message release];
    [self.wheel stopAnimating];
	self.view.backgroundColor=[UIColor whiteColor];
    NSLog(@"delegate - error(%d): %@", [error code], [error localizedDescription]);
}

#pragma mark Photo Stuff

-(void)attach:(id)sender {
	
	if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
		UIImagePickerController *picker = [[UIImagePickerController alloc] init];
		picker.delegate = self;
		picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
		[self presentModalViewController:picker animated:YES];
		[picker release];
	}
	
}
	
-(void)imagePickerController:(UIImagePickerController *)picker 
			didFinishPickingImage:(UIImage *)limage 
					editingInfo:(NSDictionary *)editingInfo 
	{
		imageView.image = limage;
		self.image = limage;
		[self.image retain];
		self.button.hidden = YES;
		[picker dismissModalViewControllerAnimated:YES];
	}



-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
	NSLog(@"Canceled");
	[picker dismissModalViewControllerAnimated:YES];
}



/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	[emailText becomeFirstResponder];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[image release];
	[emailText release];
    [super dealloc];
}


@end
